package com.sunny.ihealth.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.jaeger.library.StatusBarUtil;
import com.squareup.picasso.Picasso;
import com.sunny.ihealth.BaseActivity;
import com.sunny.ihealth.R;
import com.sunny.ihealth.ui.view.MyViewPager;

import uk.co.senab.photoview.PhotoViewAttacher;


public class PhotoViewActivity extends BaseActivity {

    private ViewPager viewPagerPhotoView;

    private String[] imgsPath;
    private View[] viewImgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_view);

        StatusBarUtil.justTransparentStatusBar(this);

        initViews();
    }


    @Override
    public void initViews() {
        Intent intent = getIntent();
        int position = 0;
        if (intent != null) {
            imgsPath = intent.getStringArrayExtra("imgs");
            position = intent.getIntExtra("position", 0);
        }
        if (imgsPath == null || imgsPath.length == 0) {
            Toast.makeText(PhotoViewActivity.this, "没有可显示的图片", Toast.LENGTH_SHORT).show();
            return;
        }
        viewImgs = new View[imgsPath.length];
        for (int i = 0; i < imgsPath.length; i++) {
            viewImgs[i] = LayoutInflater.from(this).inflate(R.layout.view_photo_view, null);
        }

        viewPagerPhotoView = (MyViewPager) findViewById(R.id.viewpager_photo_view);
        viewPagerPhotoView.setAdapter(pagerAdapter);
        viewPagerPhotoView.setCurrentItem(position, false);


    }

    private PagerAdapter pagerAdapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return viewImgs.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(viewImgs[position]);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewImgs[position]);
            ImageView ivPhotoView = (ImageView) viewImgs[position].findViewById(R.id.iv_photo_view);
            Picasso.with(PhotoViewActivity.this).load(imgsPath[position]).into(ivPhotoView);
            new PhotoViewAttacher(ivPhotoView).setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
                @Override
                public void onViewTap(View view, float x, float y) {
                    finish();
                }
            });
            return viewImgs[position];
        }
    };


}
